import { defineStore } from "pinia";
export const useCounterStore = defineStore("counter", {
  //数据集中管理
  state: () => ({
    inp: "",
    arr: [
      { status: false, id: 1, sTrue: false, title: "111" },
      { status: false, id: 2, sTrue: false, title: "222" },
      { status: false, id: 3, sTrue: false, title: "333" },
    ],
    newArr:[]
  }),
  //异步提交方法
  actions: {
    add() {
      this.arr.push({
        id: new Date().getTime(),
        status: false,
        sTrue: false,
        title: this.inp,
      });
      this.inp = "";
      this.newArr=this.arr
    },
    del(id){
      let i=this.arr.findIndex(item=>item.id==id)
      this.arr.splice(i,1)
    },
    all(){
      this.arr=this.newArr
    },
    checked(){
      this.arr=this.newArr.filter(item=>item.status)
    },
    check(){
      this.arr=this.newArr.filter(item=>!item.status)
    }
  },
  //类似于计算属性
  getters: {
    sum(state){
      return state.arr.filter(item=>!item.status).length
    }
  },
});
